package com.example.friend.controller.user;

import com.example.common.core.constants.HttpConstants;
import com.example.common.core.controller.BaseController;
import com.example.common.core.domain.Result;
import com.example.common.core.domain.vo.LoginUserVO;
import com.example.friend.domain.user.dto.UserDTO;
import com.example.friend.domain.user.dto.UserUpdateDTO;
import com.example.friend.domain.user.vo.UserVO;
import com.example.friend.service.user.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController extends BaseController {
    @Autowired
    private IUserService userService;

    //获取验证码
    @PostMapping("/sendCode")
    public Result<Void> sendCode(@RequestBody UserDTO userDTO){
        return toResult(userService.sendCode(userDTO));
    }

    //登陆
    @PostMapping("/code/login")
    public Result<String> codeLogin(@RequestBody UserDTO userDTO){
        return Result.ok(userService.codeLogin(userDTO));
    }

    //退出登陆
    @DeleteMapping("/logout")
    public Result<Void> logout(@RequestHeader(HttpConstants.AUTHENTICATION) String token){
        return toResult(userService.logout(token));
    }

    //获取用户信息
    //获取当前用户信息
    @GetMapping("/info")
    public Result<LoginUserVO> info(@RequestHeader(HttpConstants.AUTHENTICATION) String token){
        return userService.info(token);
    }

    //用户详情
    @GetMapping("/detail")
    public Result<UserVO> detail(){
        return Result.ok(userService.detail());
    }

    //修改用户信息
    @PutMapping("/edit")
    public Result<Void> edit(@RequestBody UserUpdateDTO userUpdateDTO){
        return toResult(userService.edit(userUpdateDTO));
    }

    //用户头像上传
    @PutMapping("/head-image/update")
    public Result<Void> updateHeadImage(@RequestBody UserUpdateDTO userUpdateDTO){
        return toResult(userService.updateHeadImage(userUpdateDTO.getHeadImage()    ));
    }
}
